package cn.lll.vuehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.entity.RolePermission;
import cn.lll.vuehouse.mapper.RolePermissionMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lll.vuehouse.entity.Permission;
import cn.lll.vuehouse.service.PermissionService;
import cn.lll.vuehouse.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus-pc
* @description 针对表【permission】的数据库操作Service实现
* @createDate 2023-09-03 11:05:47
*/
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public ResponseResult getAuthTree() {
        ResponseResult responseResult = new ResponseResult();

        List<Permission> permissions = permissionMapper.selectList(null);

        System.out.println("permissions = " + permissions);
        responseResult.success(ResponseStatus.SUCCESS,permissions);
        return responseResult;
    }

    @Override
    public ResponseResult getPermissionById(Integer id) {
        ResponseResult responseResult = new ResponseResult();

        Permission permission = permissionMapper.selectById(id);

        // 根据pid查询父菜单
        Integer pid = permission.getPid();
        HashMap<String, Object> map = new HashMap<>();
        if(pid==0){
            // 一级菜单
            map.put("permission",permission);
            responseResult.success(ResponseStatus.SUCCESS,map);
            return responseResult;
        }

        // 根据pid 查询title
        String title = permissionMapper.selectById(pid).getTitle();
        map.put("permission",permission);
        map.put("parentTitle",title);
        responseResult.success(ResponseStatus.SUCCESS,map);
        return responseResult;
    }

    @Override
    public ResponseResult deletePermissionById(Integer id) {
        ResponseResult responseResult = new ResponseResult();

        // 判断当前菜单是否有子菜单
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getPid,id);
        List<Permission> permissions = permissionMapper.selectList(lambda);
        if(ObjectUtil.isNotEmpty(permissions)){
            // 有，不能删
            responseResult.fail(ResponseStatus.HAS_CHILDNODE);
            return responseResult;

        }


        // 根据id删除权限表信息
        permissionMapper.deleteById(id);

        // 删除角色权限关联表信息
        LambdaQueryWrapper<RolePermission> lambda1 = new QueryWrapper<RolePermission>().lambda();
        lambda1.eq(RolePermission::getPid,id);
        rolePermissionMapper.delete(lambda1);
        responseResult.success(ResponseStatus.DELETE_SUCCESS);


        return responseResult;
    }


    @Override
    public ResponseResult getRolePermissionById(Integer id) {
        ResponseResult responseResult = new ResponseResult();

        // 根据id查询角色权限关联表获取pid
        LambdaQueryWrapper<RolePermission> lambda = new QueryWrapper<RolePermission>().lambda();
        lambda.eq(RolePermission::getRid,id);
        List<Integer> pids = rolePermissionMapper.selectList(lambda).stream().map(rp -> rp.getPid()).collect(Collectors.toList());

        // 判断pids是否为空
        if(pids.size()==0){
            // 返回前端一个空数组
            responseResult.success(ResponseStatus.SUCCESS,new ArrayList<>());
            return responseResult;
        }

        // 根据pid 查询权限表 筛选按钮级别
        LambdaQueryWrapper<Permission> lambda1 = new QueryWrapper<Permission>().lambda();
        lambda1.in(Permission::getId,pids)
                .eq(Permission::getIsMenu,2);
        List<Integer> btnIds = permissionMapper.selectList(lambda1).stream().map(p -> p.getId()).collect(Collectors.toList());
        System.out.println("btnIds = " + btnIds);
        responseResult.success(ResponseStatus.SUCCESS,btnIds);


        return responseResult;
    }

    @Override
    public ResponseResult getPermissionByTitle(String title) {
        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getTitle,title);
        Permission permission = permissionMapper.selectOne(lambda);
        responseResult.success(ResponseStatus.SUCCESS,permission);

        return responseResult;
    }
}




